﻿using ServiceStack.OrmLite;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeHouse.Server.Dal
{
    public class BaseDAL
    {

        #region 数据库配置

        public BaseDAL()
        {
            //设置方言
            OrmLiteConfig.DialectProvider = ServiceStack.OrmLite.SqlServerDialect.Provider;
            //设置命名策略
            OrmLiteConfig.DialectProvider.NamingStrategy = new PrefixLowercaseNamingStrategy() { TablePrefix = "" };
        }

        /// <summary>
        /// 数据库连接
        /// </summary>
        public IDbConnection DbConnection
        {
            get
            {
                return DbFactory.OpenDbConnection();
            }
        }

        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public virtual string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            }
        }

        private OrmLiteConnectionFactory conFactory;
        /// <summary>
        /// 连接工厂
        /// </summary>
        public OrmLiteConnectionFactory DbFactory
        {
            get
            {
                if (null == conFactory)
                {
                    conFactory = new OrmLiteConnectionFactory(ConnectionString, ServiceStack.OrmLite.SqlServerDialect.Provider);
                }
                return conFactory;
            }
        }

        #endregion

    }
}
